package com.aduerr.v1shouyin.mvp.activity;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.mvp.fragment.KaquanHeXiaoFragment;
import com.aduerr.v1shouyin.mvp.fragment.KouBeiHeXiaoFragment;

import java.util.ArrayList;

/**
 * 平台核销记录页面
 */

public class HexiaoRecordActivity extends AppCompatActivity {


    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_pingtaihexiao)
    TextView tvPingtaihexiao;
    @BindView(R.id.tv_koubeihexiao)
    TextView tvKoubeihexiao;
    @BindView(R.id.vp)
    ViewPager vp;
    private int type = 0;//0 左边的 按钮   1右边的 按钮
    private ArrayList<Fragment> fragments;
    private HexiaoRecordActivity.myAdpter myAdpter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hexiao_record);
        ButterKnife.bind(this);
        initUI();
    }

    private void initUI() {
        tvTitle.setText("核销记录");
        tvRight.setText("筛选");
        tvRight.setVisibility(View.GONE);
        tvPingtaihexiao.setSelected(true);
        fragments = new ArrayList<>();
        fragments.add(new KaquanHeXiaoFragment());
        fragments.add(new KouBeiHeXiaoFragment());
        myAdpter = new myAdpter(getSupportFragmentManager());
        vp.setAdapter(myAdpter);
        vp.setCurrentItem(0);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position == 0) {
                    tvPingtaihexiao.setSelected(true);
                    tvKoubeihexiao.setSelected(false);
                } else {
                    tvPingtaihexiao.setSelected(false);
                    tvKoubeihexiao.setSelected(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.img_back, R.id.tv_pingtaihexiao, R.id.tv_koubeihexiao})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_pingtaihexiao:
                tvPingtaihexiao.setSelected(true);
                tvKoubeihexiao.setSelected(false);
                vp.setCurrentItem(0);
                break;

            case R.id.tv_koubeihexiao:
                tvPingtaihexiao.setSelected(false);
                tvKoubeihexiao.setSelected(true);
                vp.setCurrentItem(1);
                break;
        }
    }


    class myAdpter extends FragmentPagerAdapter {

        public myAdpter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments != null ? fragments.size() : 0;
        }
    }
}
